package com.tt.jshb.auth.main;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.hzecool.common.utils.SPUtils;
import com.hzecool.core.base.TBasePresenter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tt.jshb.app.Constants;
import com.tt.jshb.app.ServerUrls;
import com.tt.jshb.bean.AuthStatusBean;
import com.tt.jshb.bean.BaseRep;
import com.tt.jshb.bean.WebAuthBean;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author tutu
 * @time 2018/6/7
 */

public class AuthPresenter extends TBasePresenter<IAuthView> {
    @Override
    protected void start() {

    }

    public void getUrl(String url) {
        OkGo.post(ServerUrls.ROUTER + url)
                .params("tokens", SPUtils.getString(Constants.SP_TOKENDS))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            WebAuthBean bean = JSON.parseObject(s, WebAuthBean.class);
                            if (bean.getCode() == 0) {
                                getView().onGetAuthUrlOk(bean.getData().getUrl());
                            } else {
                                getView().onLoadError(bean.getMsg());
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }

    public void authStatus() {
        OkGo.post(ServerUrls.ROUTER + "app/getUserMark.htm")
                .params("tokens", SPUtils.getString(Constants.SP_TOKENDS))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            AuthStatusBean bean = JSON.parseObject(s, AuthStatusBean.class);
                            Constants.APIEKY = bean.getData().getAPIKey();
                            Constants.APISECRET = bean.getData().getAPISecret();

                            if (TextUtils.isEmpty(Constants.APIEKY) || TextUtils.isEmpty(Constants.APISECRET)) {
                                getView().onAPIKeyError();
                            }

                            if (bean.getCode() == 0) {
                                getView().onAuthStatusOk(bean);
                            } else {
                                getView().onLoadError(bean.getMsg());
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }

    public void finishAuthStatus() {
        OkGo.post(ServerUrls.ROUTER + "app/getUserMark_qr.htm")
                .params("tokens", SPUtils.getString(Constants.SP_TOKENDS))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            BaseRep rep = JSON.parseObject(s, BaseRep.class);
                            if (rep.getCode() == 0) {
                                getView().canNext();
                            } else {
                                getView().onLoadError(rep.getMsg());
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }
}
